<template>
  <div class="app-container">
    <div v-if="cokies!='admin'">
      <h1>没有权限</h1>
    </div>
    <div v-if="cokies=='admin'">
      <div class="filter-container" style="margin-bottom:10px">
        <el-input
          v-model="keywords"
          placeholder="关键字"
          style="width: 200px"
        />
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search(keywords)">搜索</el-button>
        <el-button
          class="filter-item"
          style="margin-left: 10px"
          type="primary"
          icon="el-icon-edit"
          @click="openAdd"
        >新建</el-button>
      </div>
      <el-table :data="list" border fit highlight-current-row>
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">{{ scope.$index }}</template>
        </el-table-column>
        <el-table-column label="ID" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="用户Id" align="center">
          <template slot-scope="scope">{{ scope.row.userId }}</template>
        </el-table-column>
        <el-table-column label="用户名" align="center">
          <template slot-scope="scope">{{ scope.row.userName }}</template>
        </el-table-column>
        <el-table-column label="密码" align="center">
          <template slot-scope="scope">{{ scope.row.passWord }}</template>
        </el-table-column>
        <el-table-column label="真实姓名" align="center">
          <template slot-scope="scope">{{ scope.row.realName }}</template>
        </el-table-column>
        <el-table-column label="用户Id" align="center">
          <template slot-scope="scope">{{ scope.row.userPhone }}</template>
        </el-table-column>

        <el-table-column label="身份状态" align="center" width="110" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.userIdentity==1" type="success" effect="light">老师</el-tag>
            <el-tag v-if="scope.row.userIdentity==2" type="warning" effect="light">学生</el-tag>
            <el-tag v-if="scope.row.userIdentity==0" type="danger" effect="light">管理员</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="openEdit(row.id)">
              修改
            </el-button>
            <el-button size="mini" type="danger" @click="isDelete(row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display:flex">
        <el-pagination
          background
          layout="sizes, prev, pager, next, jumper, ->, total, slot"
          :total="total"
          @current-change="currentChange"
          @size-change="sizeChange"
        />
      </div></div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'

export default {
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      size: 10,
      cokies: ''
    }
  },
  // 启动自动执行该方法
  created() {
    this.fetchData()
  },
  methods: {
    openAdd() {
      this.$router.push('/example/addUser')
    },
    openEdit(id) {
      this.$router.push({ name: 'EditUser', params: { id: id }})
    },
    sizeChange(pageSize) {
      this.size = pageSize
      this.fetchData()
    },
    currentChange(currentPage) {
      this.page = currentPage
      this.fetchData()
    },
    async fetchData() {
      const { data: res } = await this.$axios.get('user/getUserList?page=' + this.page + '&size=' + this.size)
      // console.log(res)
      this.list = res.data
      this.total = res.total
      this.cokies = Cookies.get('username')
      this.$message({
        type: 'success',
        message: '查询成功!'
      })
    },
    isDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.delete(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delete(id) {
      this.$axios.delete('user/deleteUser?id=' + id)
        .then(res => {
          this.fetchData()
        })
    }
  }
}
</script>
